<?php

	include_once('../sys/sys.php');
	header('Content-Type: text/html; charset=utf-8');
	
	$action = $_GET['action'];
	$spc = $_GET['spc'];
	if($action != 'acr'){
		show_normal_mesR('操作错误','操作错误','操作错误');
	}
	//获取已经完结的和未完结的总页面数
	$wjcontent = mb_convert_encoding(file_get_contents("http://www.nonadm.com/list/zt/wj_1.htm"),"UTF-8","GB2312");
	$lzcontent = mb_convert_encoding(file_get_contents("http://www.nonadm.com/list/zt/lz_1.htm"),"UTF-8","GB2312");
	
	$wjc = explode('<div class="tys">[共<SPAN>',$wjcontent);
	$lzc = explode('<div class="tys">[共<SPAN>',$lzcontent);
	$wjc = $wjc[1];
	$lzc = $lzc[1];
	$lzc = explode('</SPAN>页]',$lzc);
	$wjc = explode('</SPAN>页]',$wjc);
	$twjc = $wjc[0];
	$tlzc = $lzc[0];
	unset($wjc);
	unset($lzc);
	unset($wjcontent);
	unset($lzcontent);
	//开始整站采集
	//定义系统预置URL
	//完结的
	$pre_wj_url = "http://www.nonadm.com/list/zt/wj_";
	$pre_lz_url = "http://www.nonadm.com/list/zt/lz_";
	$end_flg = '.htm';
	if($spc == 'wj'){
		getwj($twjc,$pre_wj_url,$end_flg);
	}else if($spc == 'lz'){
		getlz($tlzc,$pre_lz_url,$end_flg);
	}else if($spc == 'all'){
		getlz($tlzc,$pre_wj_url,$end_flg);
		getwj($twjc,$pre_wj_url,$end_flg);
	}
	
	echo "采集完毕";
	
	ob_end_flush();
	
	
	//以下为函数定义
	
	
	
	function getwj($count,$pre,$end){
		//开始循环采集每个页面
		for($i = 1;$i < $count ;$i++){
			$content = mb_convert_encoding(file_get_contents($pre . $i . $end),"UTF-8","GB2312");
			$content = explode('<DIV class=lastnew02 id=lastnew01 style="DISPLAY: none">',$content);
			$content = explode('<div class="lastnew02" id="lastnew02"> ',$content[1]);
			$content = $content[0];
			$sstr = array('<UL class=cmimg_lastimg02><LI><A class=cmimg02 ','target=_blank','</A> </LI><LI><H2><A title=');
			$rstr = array("\r\n","\r\n","\r\n");
			$content = str_replace($sstr,$rstr,$content);
			
			//直接获取连接
			$match_str = "/title=(.*) href=\"(.*)\"/isU";
			preg_match_all($match_str,$content,$urln);
			$urln = $urln[2];
			foreach($urln as $k=>$v){
				$thisurl = "http://www.nonadm.com" . $v . "/";
				//开始单页采集
				ob_flush();
				flush();
				echo '当前开始采集' . $thisurl . "<br>";
				
				//continue;
				

				getsig($thisurl);
			}
		}
		unset($content);
		
	}
	
	function getlz($count,$pre,$end){
		//开始循环采集每个页面
		for($i = 1;$i < $count ;$i++){
			$content = mb_convert_encoding(file_get_contents($pre . $i . $end),"UTF-8","GB2312");
			$content = explode('<DIV class=lastnew02 id=lastnew01 style="DISPLAY: none">',$content);
			$content = explode('<div class="lastnew02" id="lastnew02"> ',$content[1]);
			$content = $content[0];
			$sstr = array('<UL class=cmimg_lastimg02><LI><A class=cmimg02 ','target=_blank','</A> </LI><LI><H2><A title=');
			$rstr = array("\r\n","\r\n","\r\n");
			$content = str_replace($sstr,$rstr,$content);
			
			//直接获取连接
			$match_str = "/title=(.*) href=\"(.*)\"/isU";
			preg_match_all($match_str,$content,$urln);
			$urln = $urln[2];
			foreach($urln as $k=>$v){
				$thisurl = "http://www.nonadm.com" . $v . "/";
				//开始单页采集
				ob_flush();
				flush();
				echo '当前开始采集' . $thisurl . "<br>";
				
				//continue;
				

				getsig($thisurl);
			}
		}
		unset($content);
	}
	
	function getsig($url){
		global $DB;
		$id = explode("/",$url);
		if ($id[count($id) - 1] == "") {
			$id = $id[count($id) - 2];
		}
		else 
			$id = $id[count($id)-1];
			
		//定义公用变量
		if (substr($url,-1)!="/")
			$url.="/";
		$url_pre = $url;
		$pic_url_pre = 'http://www.nonadm.com/comicdata/';
			
			
		//获取URL内容
		//$content = 	file_get_contents($url);
		$content = 	mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
		
		//获取名称
		$name = explode('</title>',$content);
		$name = $name[0];
		$name = explode('<title>',$name);
		$name = $name[1];
		$name = explode('--',$name);
		$name = $name[0];
		//获取最后更新时间
		$dt = explode('更新时间：<span>',$content);
		$dt = $dt[1];
		$dt = explode('</span>',$dt);
		$dt = strtotime(trim($dt[0]));
		//获取URL列表
		$urls = explode('<ul class="bgs" >',$content);
		$urls = $urls[1];
		$urls = explode('</ul>',$urls);
		$urls = $urls[0];
		$replacer = array('target="_blank"','<span>','</span>',' class="tpy"','页');
		$replacerc = array('','','','','');
		$urls = str_replace($replacer,$replacerc,$urls);
		$match_str = "/<a href=\"(.*)\" >/isU";
		preg_match_all($match_str,$urls,$urln);
		$urln = $urln[1];
		//获取每单位总数
		$match_str = "/\((.*)\)/isU";
		preg_match_all($match_str,$urls,$numn);
		$numn = $numn[1];
		$ulnum = array($urln,$numn);
		//进入每页进行处理
		//插入分类信息
			//检查当前ID是否存在
			$where = 'AND id= ' . $id .'';
			$result = $DB->build_and_exec_query(
				array('select' => 'COUNT(*) as max','from'   => 'nonadmtags','where'	=> "1 " . $where ,)
			);	
			//当已经存在的时候更新日期
			if ($result['max'] > 0) {
				
			}
			//当不存在时插入
			else{
				do_taginsert($id,$dt,$name,0);
			}
		//获取数据库COUNT
		$where = 'AND id= ' . $id .'';
			$result = $DB->build_and_exec_query(
				array('select' => 'count','from'   => 'nonadmtags','where'	=> "1 " . $where ,)
			);	
		$dbcount = $result['count'];
		
		//由于是逆序，需要倒装数组(链接以及每个链接的数量)
        $urln = array_reverse($urln);
        $numn = array_reverse( $numn );
		
		
		foreach ($urln as $k=>$v) {
			//当COUNT小于已经采集的时候，跳过。
			if ($k < $dbcount) {
				continue;
			}
			$current_url = $url_pre . $v;
			$comic_contents = mb_convert_encoding(file_get_contents($current_url),"UTF-8","GB2312");
			//获取中间路径
			$dtd = explode('var volpic=\'',$comic_contents);
			$dtd = $dtd[1];
			$dtd = explode('\';var tpf',$dtd);
			$dtd = $dtd[0];
			$complete_url = $pic_url_pre . $dtd;
			//这里添加数据库插入语句
			do_photoinsert($id,$complete_url,$numn[$k]);
		}
		//更新计数
		$where = 'id= ' . $id .'';
		$DB->do_update('nonadmtags',
			array(					  
			   'lastupdate'				=> $dt,
			   'count'					=> count($urln),
			 ),
			 $where
		);
		//注销使用的变量
		unset($content);
		unset($ulnum);
		unset($urln);
		unset($numn);
	}
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		$DB->do_insert( 'nonadmtags', array( 'id'			=> $id,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($uid,$url,$count){
		global $DB;
		$DB->do_insert( 'nonadmphotos', array( 
								   'uid'				=> $uid,
								   'url'					=> $url,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	
?>